﻿using Enums;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using XF.Common;
using XF.ExControls;

namespace KYBM
{
    public partial class FrmCustomerList : Form
    {
        XF.BLL.Base_Customer bll = new XF.BLL.Base_Customer();
        int iVerticalScrollIndex = 0;

        public FrmCustomerList()
        {
            InitializeComponent();
        }

        private void FrmStudentList_Load(object sender, EventArgs e)
        {
            xfDataGridView1.AutoGenerateColumns = false;
            ColPayType.DisplayMember = "Text";
            ColPayType.ValueMember = "Value";
            ColPayType.DataSource = GenerateData.GetPayType(false);
            BindData();
        }

        private void tsBtnSearch_Click(object sender, EventArgs e)
        {
            pagerControl1.PageIndex = 1;
            BindData();
        }

        private void BindData()
        {
            int sum = 0;
            string strWhere = string.Empty;
            if(!string.IsNullOrEmpty(tsTbCustomer.Text.Trim()))
            {
                strWhere += " and (CustomerCode like '%" + tsTbCustomer.Text.Trim() + "%' or CustomerName like '%" + tsTbCustomer.Text.Trim() + "%')";
            }
            List<XF.Model.Base_Customer> models = bll.GetModelListByPage(strWhere, " CustomerCode Asc ", pagerControl1.PageIndex, pagerControl1.PageSize, ref sum);
            pagerControl1.DrawControl(sum);
            xfDataGridView1.Rows.Clear();
            foreach (XF.Model.Base_Customer model in models)
            {
                int count = xfDataGridView1.RowCount;
                xfDataGridView1.Rows.Add();
                xfDataGridView1.Rows[count].Cells[ColId.Name].Value = model.Id;
                xfDataGridView1.Rows[count].Cells[ColCode.Name].Value = model.CustomerCode;
                xfDataGridView1.Rows[count].Cells[ColName.Name].Value = model.CustomerName;
                xfDataGridView1.Rows[count].Cells[ColPayType.Name].Value = model.PayType;
                xfDataGridView1.Rows[count].Cells[ColContacts.Name].Value = model.Contacts;
                xfDataGridView1.Rows[count].Cells[ColPhone.Name].Value = model.Phone;
                xfDataGridView1.Rows[count].Cells[ColNo.Name].Value = zDataConverter.ToInt(model.No);
                xfDataGridView1.Rows[count].Cells[ColAccount.Name].Value = model.Account;
                xfDataGridView1.Rows[count].Cells[ColAddress.Name].Value = model.Address;
                xfDataGridView1.Rows[count].Cells[ColRemark.Name].Value = model.Remark;
            }
        }
        private void pagerControl1_OnPageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private void tsBtnAdd_Click(object sender, EventArgs e)
        {
            FrmCustomerCard frmCustomerCard = new FrmCustomerCard();
            if(frmCustomerCard.ShowDialog() == DialogResult.OK)
            {
                if (bll.Add(frmCustomerCard.Customer) > -1)
                {
                    QQMessageBox.Show(
                                this,
                                MessageText.TIP_SUCCESS_ADD,
                                MessageText.MESSAGEBOX_CAPTION_TIP,
                                QQMessageBoxIcon.OK,
                                QQMessageBoxButtons.OK);
                    BindData();
                    xfDataGridView1.FirstDisplayedScrollingRowIndex = iVerticalScrollIndex;
                }
                else
                {
                    QQMessageBox.Show(
                                this,
                                MessageText.TIP_ERROR_ADD,
                                MessageText.MESSAGEBOX_CAPTION_TIP,
                                QQMessageBoxIcon.Information,
                                QQMessageBoxButtons.OK);
                }
            }
        }

        private void tsBtnUpdate_Click(object sender, EventArgs e)
        {
            List<int> indexs = xfDataGridView1.GetSelectedRowsIndex();
            if (indexs.Count == 1)
            {
                int id = zDataConverter.ToInt(xfDataGridView1.Rows[indexs[0]].Cells[ColId.Name].Value);
                FrmCustomerCard frmCustomerCard = new FrmCustomerCard();
                frmCustomerCard.Customer = bll.GetModel(id);
                if(frmCustomerCard.ShowDialog() == DialogResult.OK)
                {
                    if (bll.Update(frmCustomerCard.Customer))
                    {
                        QQMessageBox.Show(
                                    this,
                                    MessageText.TIP_SUCCESS_UPDATE,
                                    MessageText.MESSAGEBOX_CAPTION_TIP,
                                    QQMessageBoxIcon.OK,
                                    QQMessageBoxButtons.OK);
                        BindData();
                        xfDataGridView1.FirstDisplayedScrollingRowIndex = iVerticalScrollIndex;
                    }
                    else
                    {
                        QQMessageBox.Show(
                                    this,
                                    MessageText.TIP_ERROR_UPDATE,
                                    MessageText.MESSAGEBOX_CAPTION_TIP,
                                    QQMessageBoxIcon.Information,
                                    QQMessageBoxButtons.OK);
                    }
                }
            }
            else
            {
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_SELECTCOUNT_ONE,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
            }
        }

        private void tsBtnDelete_Click(object sender, EventArgs e)
        {
            RowsDelete();
        }

        /// <summary>
        /// 多行记录删除
        /// </summary>
        private void RowsDelete()
        {
            List<int> indexs = xfDataGridView1.GetSelectedRowsIndex();
            if (indexs.Count > 0)
            {
                string sIDs = string.Empty;
                string sID;
                //构建删除ID
                foreach (int i in indexs)
                {
                    sID = zDataConverter.ToString(xfDataGridView1.Rows[i].Cells[ColId.Name].Value);
                    if (sID.Length > 0)
                    {
                        sIDs += MessageText.KEY_COMMA + sID;
                    }
                }
                if (sIDs.Length > 0)
                {
                    //执行数据库删除
                    if (!bll.DeleteList(sIDs.Substring(1)))
                    {
                        QQMessageBox.Show(
                        this,
                        MessageText.TIP_SUCCESS_DELETE,
                        MessageText.MESSAGEBOX_CAPTION_ERROR,
                        QQMessageBoxIcon.Error,
                        QQMessageBoxButtons.OK);
                        return;
                    }
                }
                BindData();
                xfDataGridView1.FirstDisplayedScrollingRowIndex = iVerticalScrollIndex;
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_SUCCESS_DELETE,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.OK,
                            QQMessageBoxButtons.OK);
            }
            else
            {
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_SELECTCOUNT_NULL,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
            }
        }

        private void xfDataGridView1_Scroll(object sender, ScrollEventArgs e)
        {
            if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
            {
                iVerticalScrollIndex = e.NewValue;
            }
        }
    }
}
